package com.spaceraccoons.client;

import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.sixfire.websocket.WebSocket;
import com.spaceraccoons.api.ProtobufApiAdapterModule;
import com.spaceraccoons.api.adapter.DateAdapter;
import com.spaceraccoons.api.adapter.DateTimeAdapter;

/**
 * 
 * @author Benjamin P. Jung
 */
public class Client {

	private final Logger logger = LoggerFactory.getLogger(Client.class);

	@Inject private DateAdapter dateAdapter;
	@Inject private DateTimeAdapter dateTimeAdapter;

	WebSocket webSocket;

	public Client() {
		super();
	}
	
	public Client(final String url) {
		this();
		this.setUrl(url);
	}

	public void setUrl(final String url) {
		if (isConnected()) {
			throw new IllegalStateException("Already connected.");
		}
		this.webSocket = new WebSocket(url);
	}

	public void connect() throws IOException {
		if (this.webSocket == null) {
			throw new IllegalStateException("Server URL has not yet been set.");
		}
		webSocket.connect();
	}
	
	protected void ping() {
		
	}

	public boolean isConnected() {
		return webSocket != null && webSocket.isConnected();
	}

	public static void main(String[] args) throws Exception {
		
		final Injector guice = Guice.createInjector(
				new ProtobufApiAdapterModule());
		
		final Client client = guice.getInstance(Client.class);
		client.setUrl("ws://localhost:8080/");
		client.connect();
		client.webSocket.send("ping!");
	}

}
